package com.quectel.taskserver.job.application;

import com.quectel.business.dms.application.ApplicationBusiness;
import com.quectel.core.module.application.dto.ApplicationDto;
import com.quectel.core.module.application.service.ApplicationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定时任务同步设备
 *
 * @author caolu
 * @email louis.cao@quectel.com
 */
@Component
public class ApplicationInitTask implements DataflowJob<ApplicationDto> {

    @DubboReference
    private ApplicationService applicationService;
    @Autowired
    private ApplicationBusiness applicationBusiness;

    @Override
    public List<ApplicationDto> fetchData(ShardingContext shardingContext) {

        return applicationService
                .fetchNeedTaskDataBySharding(shardingContext.getShardingTotalCount(), shardingContext.getShardingItem());
    }

    @Override
    public void processData(ShardingContext shardingContext, List<ApplicationDto> list) {
        for (ApplicationDto applicationDto : list) {
            //更新最后一次执行时间
            ApplicationDto updateObj = new ApplicationDto();
            updateObj.setId(applicationDto.getId());
            updateObj.setLastExecTaskTime(new Date());
            applicationService.updateById(updateObj);

            applicationBusiness.init(applicationDto, true);
        }
    }
}
